package cn.fl.prjprddtlgps.controller;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.commons.result.ResponseData;
import cn.fl.deviceinfo.api.IDeviceInfoApiService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.IPrjPrdDtlGpsApiService;
import cn.fl.preloan.condition.PrjPrdDtlGpsQueryCondition;
import cn.fl.preloan.dto.gps.GpsDeviceDTO;
import cn.fl.prjprddtlgps.condition.PrjPrdDtlGpsQC;
import cn.fl.prjprddtlgps.dto.PrjPrdDtlGpsDTO;
import cn.fl.prjprddtlgps.service.IPrjPrdDtlGpsService;
import cn.fl.prjprddtlgps.transform.PrjPrdDtlGpsVOTransform;
import cn.fl.prjprddtlgps.vo.PrjPrdDtlGpsVO;


import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdDtlVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: PRJ_PRD_DTL_GPS（重卡其他GPS） 控制层
 */
@Controller
@RequestMapping("/prjprddtlgps/prjPrdDtlGps")
public class PrjPrdDtlGpsController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjPrdDtlGpsController.class);

	/**
	 * PrjPrdDtlGps 服务层BEAN
	 */
	@Autowired
	private IPrjPrdDtlGpsService prjPrdDtlGpsService;

	@Autowired
	private IPrjPrdDtlService prjPrdDtlService;

	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	@Autowired
	IDeviceInfoApiService deviceInfoApiService;

	/**
	 * 系统用户
	 */
	@Autowired
	private ISysUsrApiService sysUsrApiService;

	@Autowired
	private IPrjPrdDtlGpsApiService prjPrdDtlGpsApiService;

	/**
	 * 对比附件是否存在
	 */
	@RequestMapping(value = "/compareFilesIsExists.do", method = { RequestMethod.GET})
	public String compareFilesIsExists() {
		return this.prjPrdDtlGpsService.compareFilesIsExists();
	}
	
	/**
	 * 对比附件是否存在
	 */
	@RequestMapping(value = "/compareFilesExists.do", method = { RequestMethod.GET})
	public String compareFilesExists() {
		return this.prjPrdDtlGpsService.compareFilesExists();
	}

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("prjprddtlgps/prjPrdDtlGps/list");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("prjprddtlgps/prjPrdDtlGps/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjPrdDtlGpsQC qc = new PrjPrdDtlGpsQC();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.prjPrdDtlGpsService.searchListPage(page);
		List<PrjPrdDtlGpsVO> list = (List<PrjPrdDtlGpsVO>) pager.getResultList();
		ListPageVO<PrjPrdDtlGpsVO> listPage = new ListPageVO<PrjPrdDtlGpsVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}


	/**
	 * 初始化 GPS信息列表（不分页）
	 * /prjprddtlgps/prjPrdDtlGps/initGPSInfoList.do?prjDtlId=181114099030722
	 */
	@RequestMapping(value = "/initGPSInfoList.do", method = {RequestMethod.GET})
	@ResponseBody
	public Object initGPSInfoList(HttpServletRequest request, HttpServletResponse response) {
		String prjDtlId = request.getParameter("prjDtlId");// 项目产品明细id

		if (StringUtils.isBlank(prjDtlId)){
			return new ListPageVO<PrjPrdDtlGpsVO>(new ArrayList<PrjPrdDtlGpsVO>(), 0);
		}
		PrjPrdDtlGpsQueryCondition qc = new PrjPrdDtlGpsQueryCondition();
		qc.setPrjDtlId(Long.valueOf(prjDtlId));

		BaseLogUtils.info(logger, "PrjPrdDtlGpsController --> initGPSInfoList", "初始化GPS信息列表/initGPSInfoList.do:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
		List<PrjPrdDtlGpsVO> list = this.prjPrdDtlGpsService.searchList(qc);
		if(list == null){
			list = new ArrayList<>();
		}
		if (list.size() == 0){
			for (int i = 0; i < 3; i++) {
				PrjPrdDtlGpsVO vo = new PrjPrdDtlGpsVO();
				vo.setGpsCardNo("");
				vo.setSerialNo(i+1);
				list.add(vo);
			}
		} else {
			if (list.size() == 1){
				for (int i = 0; i < 2; i++) {
					PrjPrdDtlGpsVO vo = new PrjPrdDtlGpsVO();
					vo.setGpsCardNo("");
					vo.setSerialNo(i+2);
					list.add(vo);
				}
			}
			if (list.size() == 2){
				for (int i = 0; i < 1; i++) {
					PrjPrdDtlGpsVO vo = new PrjPrdDtlGpsVO();
					vo.setGpsCardNo("");
					vo.setSerialNo(i+3);
					list.add(vo);
				}
			}

		}

		PrjPrdDtlVO dtlVO = this.prjPrdDtlService.selectById(Long.valueOf(prjDtlId));
		if(dtlVO != null && dtlVO.getPrjId() != null){
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(dtlVO.getPrjId());
			if(prjBscInfoVO != null && prjBscInfoVO.getCstMgrId() != null){
				// 之前:专车客车未接入大数据 现在:全体事业部
				String cstMgrOrgCd = prjBscInfoVO.getCstMgrOrgCd();
				if(StringUtils.isNotBlank(cstMgrOrgCd)){
					prjPrdDtlGpsService.setDeviceAddInfo(prjBscInfoVO.getCstMgrId(),list);
				}
			}
		}

		if (CollectionUtils.isNotEmpty(list)) {
			return new ListPageVO<PrjPrdDtlGpsVO>(list, list.size());
		}
		return new ListPageVO<PrjPrdDtlGpsVO>(new ArrayList<PrjPrdDtlGpsVO>(), 0);
	}


	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.prjPrdDtlGpsService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			PrjPrdDtlGpsVO vo = this.prjPrdDtlGpsService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjPrdDtlGpsVO entity = null;
		try {
			entity = new PrjPrdDtlGpsVO();
			//TODO ADD PARAM
			Long id = this.prjPrdDtlGpsService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改 prjprddtlgps/prjPrdDtlGps/modify.do
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		PrjPrdDtlGpsVO entity = null;
		try {
			entity = new PrjPrdDtlGpsVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.prjPrdDtlGpsService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 修改GPS审核状态接口，放款流程中商助审核通过之后修改审核状态
	 * 同时修改GPS序号1和2的审核状态
	 * @return
	 */
	@RequestMapping(value = "/modifyGPSAudSts.do", method = {RequestMethod.POST})
	@ResponseBody
	public Object modifyGPSAuditStatus(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);

		String prjDtlId = request.getParameter("prjDtlId");// 项目产品明细id
		String gpsAudSts = request.getParameter("gpsAudSts");// GPS序号1和2的审核状态(根据prjDtlId查询出来的GPS要改成该状态)

//		PrjPrdDtlGpsVO gpsVO = new PrjPrdDtlGpsVO();
//		gpsVO.setPrjDtlId(Long.valueOf(prjDtlId));
//		gpsVO.setGpsAudStsCd(Integer.valueOf(gpsAudSts));

		boolean modifyResult = this.prjPrdDtlGpsService.modifyGpsAudStsByDtlId(Integer.valueOf(gpsAudSts),Long.valueOf(prjDtlId));
		if (!modifyResult) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "modifyGPSAudSts.do", "修改GPS审核状态失败.[prjDtlId=" + prjDtlId + "]", CurrentThreadContext.getCurrentUserName());
		}


		return result;
	}

	/**
	 * 修改GPS(还未实际放款，不需要走GPS变更流程)
	 * /prjprddtlgps/prjPrdDtlGps/modifyGPS.do
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/modifyGPS.do", method = {RequestMethod.POST})
	@ResponseBody
	public Object modifyGPS(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		String gpsNums = request.getParameter("gpsNums");// GPS卡号，数据格式如："卡号1,卡号2"
		String dtlId = request.getParameter("dtlId");
		if (StringUtils.isNotBlank(gpsNums)){
			List<PrjPrdDtlGpsVO> list = new ArrayList<>();
			String[] gpsArray = gpsNums.split(";");
			for (int i = 0; i < gpsArray.length; i++){
				PrjPrdDtlGpsVO dto = new PrjPrdDtlGpsVO();
				dto.setGpsCardNo(gpsArray[i].split(",")[0]);
				dto.setId(Long.parseLong(gpsArray[i].split(",")[1]));
				dto.setPrjDtlId(Long.valueOf(dtlId));
				list.add(dto);
			}
			//判断填写数据是否有重复
			List checkList = new ArrayList();
			if(CollectionUtils.isEmpty(list)){
				result.setSuccess(0);
				result.setInfo("无GPS卡号，请确认!");
				return result;
			}
			for(PrjPrdDtlGpsVO dto : list){
				String gpsCardNo = dto.getGpsCardNo();
				if(StringUtils.isNotBlank(gpsCardNo) && !gpsCardNo.equals(BaseConstant.IS_YESNO_NO_STR)){
					if(checkList.contains(gpsCardNo)){
						result.setSuccess(0);
						result.setInfo("填写的GPS重复，请调整!");
						return result;
					}
					checkList.add(gpsCardNo);

					Integer ret = prjPrdDtlGpsService.checkGPSExist(dto.getGpsCardNo(), dto.getPrjDtlId());
					if(ret!=1){
						result.setSuccess(0);
						result.setInfo("修改的GPS信息被其他资产占用,请调整");
						return result;
					}
				}
			}
			// 校验GPS卡号是否属于当前用户已领用
			PrjPrdDtlVO prjPrdDtlVO = this.prjPrdDtlService.selectById(Long.valueOf(dtlId));
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjPrdDtlVO.getPrjId());
			String cstMgrOrgCd = prjBscInfoVO.getCstMgrOrgCd();
			if(StringUtils.isBlank(cstMgrOrgCd)){
				result.setSuccess(0);
				result.setInfo("未查询到客户经理组织机构代码");
				return result;
			}

			// 客户经理工号
			String customManagerWorkNum = selectCustomManagerWorkNum(prjBscInfoVO);

			// 之前:专车事业部不接大数据 现在:全体事业部
//			if(!cstMgrOrgCd.startsWith("000000080002")){
				List<String> gpsDeviceList = this.prjPrdDtlGpsService.getGpsDevice(prjBscInfoVO.getCstMgrId(),"2");
				// 历史数据处理：
				PrjPrdDtlGpsQueryCondition prjPrdDtlGpsQueryCondition = new PrjPrdDtlGpsQueryCondition();
				prjPrdDtlGpsQueryCondition.setIsDel(0);
				prjPrdDtlGpsQueryCondition.setPrjDtlId(Long.valueOf(dtlId));
				List<PrjPrdDtlGpsVO> prjPrdDtlGpsVOS = prjPrdDtlGpsService.searchList(prjPrdDtlGpsQueryCondition);
				for(PrjPrdDtlGpsVO dto : list){
					String gpsCardNo = dto.getGpsCardNo();
					if(StringUtils.isNotBlank(gpsCardNo) && !gpsCardNo.equals(BaseConstant.IS_YESNO_NO_STR)){

						// 调用大数据返回实际领用人、实际领用人工号、设备类型
						ResponseData responseData = queryGpsDeviceInfoOfHadoop(prjBscInfoVO, gpsCardNo, customManagerWorkNum);
						if (null != responseData && null != responseData.getData()) {
							dto.setReceiveRealName(((GpsDeviceDTO)responseData.getData()).getReceiveRealName());
							dto.setReceiveName(((GpsDeviceDTO)responseData.getData()).getReceiveName());
							dto.setGpsDeviceType(((GpsDeviceDTO)responseData.getData()).getGpsDeviceType());
						}


						// 历史数据，已存在GPS已领用状态，大数据系统不返回，保存时不校验
						if(CollectionUtils.isNotEmpty(prjPrdDtlGpsVOS)){
							boolean containsFlag = false;
							for(PrjPrdDtlGpsVO prjPrdDtlGpsVO : prjPrdDtlGpsVOS){
								if(Objects.equals(prjPrdDtlGpsVO.getGpsCardNo(),gpsCardNo)){
									containsFlag = true;
									break;
								}
							}
							if(containsFlag){
								break;
							}
						}
						// 校验GPS卡号是否属于当前用户已领用
						if(CollectionUtils.isEmpty(gpsDeviceList)){
							result.setSuccess(0);
							result.setInfo("项目客户经理账号下未查询到设备，请核实");
							return result;
						}
						boolean flag = false;
						for(String gps : gpsDeviceList){
							if(Objects.equals(dto.getGpsCardNo(),gps)){
								flag = true;
								break;
							}
						}
						if(!flag){
							BaseLogUtils.newLogger("GPS卡号录入有误，请核实,gpsCarNo={}",gpsCardNo).info();
							result.setSuccess(0);
							result.setInfo("GPS卡号录入有误，请核实");
							return result;
						}
					}
				}
//			}

			boolean modifyResult = this.prjPrdDtlGpsService.modifyPrjPrdDtlGpsList(list);
			if (!modifyResult) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modifyGPS.do", "修改GPS信息失败.[卡号=" +gpsNums  + "]", CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.newLogger("修改GPS实时发送设备信息开始入参dtlId=" + dtlId).info();
			boolean b = deviceInfoApiService.sendDeviceInfo(Long.valueOf(dtlId));
			BaseLogUtils.newLogger("修改GPS实时发送设备信息返回结果dtlId=" + dtlId + "b=" + b).info();
		}
		return result;
	}

	/**
	 * @Author liuxiangyang
	 * @Description 获取用户名下GPS设备
	 * @Date 17:02 2020/7/28
	 * @Param 设备状态，默认0
	 * @return
	**/
	@RequestMapping(value = "/getGpsDevice.do", method = {RequestMethod.GET})
	@ResponseBody
	public Object getGpsDevice(@RequestParam(value = "status",defaultValue = "0") String status,
							   @RequestParam(value = "prjId",required = false)Long prjId,
							   @RequestParam(value = "dtlId",required = false)Long dtlId) throws BaseException {

		BaseLogUtils.newLogger("获取GPS设备参数，status={},prjId={},dtlId={}",status,prjId,dtlId).info();
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try{
			if(prjId == null && dtlId == null){
				throw new BaseException("请求参数校验错误");
			}
			if(prjId == null){
				PrjPrdDtlVO dtlVO = this.prjPrdDtlService.selectById(dtlId);
				if(dtlVO == null || dtlVO.getPrjId() == null){
					throw new BaseException("未获取到项目详情信息");
				}
				prjId = dtlVO.getPrjId();
			}
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjId);
			if(prjBscInfoVO == null && prjBscInfoVO.getCstMgrId() == null){
				throw new BaseException("未获取到项目或客户经理信息");
			}
			List<String> list = prjPrdDtlGpsService.getGpsDevice(prjBscInfoVO.getCstMgrId(),status);
			List<Map> dataList = new ArrayList<>();
			if(CollectionUtils.isNotEmpty(list)){
				for(String gpsDeviceNo : list){
					Map map = new HashMap<>();
					map.put("gpsCarNo",gpsDeviceNo);
					dataList.add(map);
				}
				ListPageVO listPage = new ListPageVO(dataList, dataList.size());
				result.setData(listPage);
			}
		}catch (BaseException e){
			result = new DataResultVO(BaseConstant.IS_YESNO_NO, e.getMessage());
			BaseLogUtils.newLogger("获取GPS设备异常", JSON.toJSONString(result)).error(LogExceptionEnum.COMMON_CODE_0002.getCode(),e);
		}catch (Exception e){
			result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_OPERATE_FAIL);
			BaseLogUtils.newLogger("获取GPS设备异常", JSON.toJSONString(result)).error(LogExceptionEnum.API_CODE_0102.getCode(),e);
		}
		BaseLogUtils.newLogger("获取GPS设备参数返回.prjId={},result={}", prjId, JSON.toJSONString(result)).info();
		return result;
	}


	/**
	 * @description 查询客户经理工号
	 * @param prjBscInfoVO 项目基本信息
	 */
	private String selectCustomManagerWorkNum(PrjBscInfoVO prjBscInfoVO) {
		List<Long> list = new ArrayList<>();
		// 客户经理id
		Long cstMgrId = prjBscInfoVO.getCstMgrId();
		list.add(cstMgrId);

		SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
		// 客户经理id
		sysUsrQueryCondition.setUsrIds(list);
		sysUsrQueryCondition.setSysCd("11000");

		List<SysUsrDTO> sysUsrDtoS = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
		BaseLogUtils.newLogger("项目id:{},查询出来的系统用户:{}",prjBscInfoVO.getId(),JSON.toJSONString(sysUsrDtoS)).info();

		if (CollectionUtils.isNotEmpty(sysUsrDtoS)) {
			for (SysUsrDTO sysUsrDto : sysUsrDtoS) {
				if (null != sysUsrDto && null != sysUsrDto.getUsrNm()) {
					return  sysUsrDto.getUsrNm();
				}
			}
		}
		return null;
	}


	/**
	 * @description 查询GPS设备信息,调用大数据返回的数据
	 * @param prjBscInfoVO  项目基本信息
	 * @param gpsCardNo gps卡号信息
	 * @param customManagerWorkNum 当前客户经理工号
	 */
	private ResponseData queryGpsDeviceInfoOfHadoop(PrjBscInfoVO prjBscInfoVO, String gpsCardNo, String customManagerWorkNum) {
		ResponseData responseData = null;

		if (StringUtils.isNotBlank(customManagerWorkNum)) {
			responseData = prjPrdDtlGpsApiService.validateGpsDeviceOfHadoop((StringUtils.isNotBlank(gpsCardNo) ? gpsCardNo.trim() : "0"), customManagerWorkNum);
			BaseLogUtils.newLogger("项目id:{},GPS卡号:{},调用大数据平台返回的数据:{}",prjBscInfoVO.getId(),gpsCardNo,null == responseData ? "错误" :JSON.toJSONString(responseData)).info();
		}
		return  responseData;
	}
}

